/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reserva_Criar.java
 *
 * Created on Jun 11, 2012, 8:19:20 PM
 */

package gui;
import business.Pessoa;
import business.Reserva;
import business.Categoria;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import vo.CategoriaVO;


/**
 *
 * @author Guto
 */
public class Reserva_Criar extends javax.swing.JPanel {
private static Reserva_Criar instance;
    /** Creates new form Reserva_Criar */
    public Reserva_Criar() {
        initComponents();
        updateCategoria();
    }
public static Reserva_Criar getInstance(){
    if(instance==null)
        instance = new Reserva_Criar();
    return instance;
}
    public void updateNome(String nome)
    {
        List<CategoriaVO> lista = Categoria.getCategoriaPeloNome(nome);
        Object[][] dados = new Object[lista.size()][2];
        
        for(int i=0;i<1;i++){
            CategoriaVO categoria =(CategoriaVO) lista.get(i);
            dados[i][0]=categoria.getNome();
            dados[i][1]=categoria.getTarifa();
            ((DefaultTableModel)jTable1.getModel()).addRow(dados[i]);
        }
        
    }
    public void updateCategoria(){
        
        List<CategoriaVO> lista = Categoria.getCategorias();
        
        Object[][] dados = new Object[lista.size()][2];
        
        for(int i=0;i<lista.size();i++)
        {
            CategoriaVO categoria = (CategoriaVO) lista.get(i);
            dados[i][0]=categoria.getNome();
            dados[i][1]=categoria.getTarifa();
            ((DefaultTableModel)jTable1.getModel()).addRow(dados[i]);
        }

    }
  private void LimpaTela()
  {
      jTextField1.setText("");
      jTextField2.setText("");
      jTextField3.setText("");
      jTextField4.setText("");
      jTextField5.setText("");
  }
    
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/Bundle"); // NOI18N
        jLabel1.setText(bundle.getString("Reserva_Criar.jLabel1.text_1")); // NOI18N

        jLabel2.setText(bundle.getString("Reserva_Criar.jLabel2.text")); // NOI18N

        jLabel3.setText(bundle.getString("Reserva_Criar.jLabel3.text")); // NOI18N

        jLabel4.setText(bundle.getString("Reserva_Criar.jLabel4.text")); // NOI18N

        jLabel5.setText(bundle.getString("Reserva_Criar.jLabel5.text")); // NOI18N

        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField1FocusLost(evt);
            }
        });

        jTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField2FocusLost(evt);
            }
        });

        jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField3FocusLost(evt);
            }
        });

        jTextField4.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField4FocusLost(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Tarifa"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText(bundle.getString("Reserva_Criar.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(bundle.getString("Reserva_Criar.jButton2.text")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel6.setText(bundle.getString("Reserva_Criar.jLabel6.text")); // NOI18N

        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel2)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField3)
                                    .addComponent(jTextField2)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(132, 132, 132)
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField5))
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(jLabel1)))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
           
              float orc = Reserva.GerarOrcamento(jTextField1.getText(),
               jTextField2.getText(), 
               (String)jTable1.getValueAt(jTable1.getSelectedRow(), 0)             
              );
       jTextField5.setText(String.valueOf(orc));
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusLost
         String Data_Retirada = jTextField1.getText(); 
         Calendar Agora= Calendar.getInstance();

         
         //int Hora_Atual=Agora.get(Calendar.HOUR);
         //int Minutos_Atual=Agora.get(Calendar.MINUTE);
         int Dia_Atual=Agora.get(Calendar.DAY_OF_MONTH);
         int Mes_Atual=Agora.get(Calendar.MONTH)+1;
         int Ano_Atual=Agora.get(Calendar.YEAR);
         
         
         int Dia_Retirada=Reserva.GeraDias(Data_Retirada);
         int Mes_Retirada=Reserva.GeraMes(Data_Retirada);
         int Ano_Retirada=Reserva.GeraAno(Data_Retirada);
        

         if(Data_Retirada.isEmpty()||Ano_Retirada<Ano_Atual || ((Ano_Retirada==Ano_Atual)&&(Mes_Retirada<Mes_Atual))  || ((Ano_Retirada==Ano_Atual)&&(Mes_Retirada==Mes_Atual)&&(Dia_Retirada<Dia_Atual))){
                JOptionPane.showMessageDialog(this, "Data inválida.");
                jTextField1.requestFocus();
                jTextField1.selectAll(); 
                return;
         }
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1FocusLost

    private void jTextField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField2FocusLost
         String Data_Retirada = jTextField2.getText(); 
         Calendar Agora= Calendar.getInstance();

         
         //int Hora_Atual=Agora.get(Calendar.HOUR);
         //int Minutos_Atual=Agora.get(Calendar.MINUTE);
         int Dia_Atual=Agora.get(Calendar.DAY_OF_MONTH);
         int Mes_Atual=Agora.get(Calendar.MONTH)+1;
         int Ano_Atual=Agora.get(Calendar.YEAR);
         
         
         int Dia_Retirada=Reserva.GeraDias(Data_Retirada);
         int Mes_Retirada=Reserva.GeraMes(Data_Retirada);
         int Ano_Retirada=Reserva.GeraAno(Data_Retirada);
        

         if(Data_Retirada.isEmpty()||Ano_Retirada<Ano_Atual || ((Ano_Retirada==Ano_Atual)&&(Mes_Retirada<Mes_Atual))  || ((Ano_Retirada==Ano_Atual)&&(Mes_Retirada==Mes_Atual)&&(Dia_Retirada<Dia_Atual))){
                JOptionPane.showMessageDialog(this, "Data inválida.");
                jTextField2.requestFocus();
                jTextField2.selectAll(); 
                return;
         }
   // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2FocusLost

    private void jTextField4FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField4FocusLost

    String CPF = jTextField4.getText();
        
    if(!Pessoa.EhCliente(CPF)){
                JOptionPane.showMessageDialog(this, "CPF inválida.");
                jTextField4.requestFocus();
                jTextField4.selectAll(); 
                return;
    }
     
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4FocusLost

    private void jTextField3FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField3FocusLost

  String nome= jTextField3.getText();

  if(!Categoria.categoriaExiste(nome)||jTextField3.getText().isEmpty())
  {
    if(jTextField3.getText().isEmpty())
    {
           ((DefaultTableModel) jTable1.getModel()).setNumRows(0);
            jTable1.updateUI();       
        updateCategoria();
    }
    else
    {
     jTextField3.requestFocus();
      jTextField3.selectAll();
      return;
    }
  }
  else
  {
          ((DefaultTableModel) jTable1.getModel()).setNumRows(0);
            jTable1.updateUI();
            updateNome(nome); 
  }

        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3FocusLost

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         boolean flag = true;
        if (
            jTextField4.getText().isEmpty()   || jTextField1.getText().isEmpty() ||
            jTextField2.getText().isEmpty() ||  jTextField5.getText().isEmpty()){
            
            JOptionPane.showMessageDialog(this,"Existem campos em branco!");
            flag = false;
        }
        //TODO Verificar se reserva já existe
        if (flag == true){
               // System.out.println(Pessoa.getPessoaCPFPorUsuario(Principal.getInstance().getFuncionarioLogado()));
                Reserva.insertReservaCriar((String)jTable1.getValueAt(jTable1.getSelectedRow(), 0),
                jTextField1.getText(),
                jTextField2.getText(),jTextField5.getText(),jTextField4.getText(),Pessoa.getPessoaCPFPorUsuario(Principal.getInstance().getFuncionarioLogado()));
        
                
                
        this.LimpaTela();
        }
               // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField5ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
